import {
	createStore
} from "vuex";
import apiLogin from '@/util/network_api/login.js'
import apiUser from '@/util/network_api/user.js'
import apiCode from '@/util/api/code.js'
import apiAddress from '@/util/network_api/address.js'
const store = createStore({
	// 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。
	state: {
		// 个人中心用户信息
		userInfo: {
			avator: '',
			name: '',
			account: '',
			nickname: '',
			phone: '',
			id: null
		},
		orderVos: [], //选择的可提现单
		key: '30f68f91c93ee366d13fba13e1e8fc32', // 高德key_H5
		// key:'e816bb252ac41dc977d56dda7476ba75',//小程序
		isManager: false, //是否管理员
		isRole: '', //角色编码
		addressList: [], //地址列表
		total: 0, //地址列表总条数
		userOrder: {
			"address": "", //客户详细地址
			"area": "", //地址-县/区
			"city": "", //地址-市
			"helpOtherCreateOrder": false, //	是否代他人建单：0-否，1-是	
			"name": "", //客户姓名
			"otherPhone": "", //其他电话
			"phone": "", //客户手机号
			"province": "", //地址-省
			"town": "", //地址-镇
			"userId": null
		}, //报单时选中显示的地址
	},
	mutations: {
		// 通过提交commit改变数据
		// 更新state数据，用于修改状态(同步)
		setStateAttr(state, info) {
			state.userInfo = {
				...info
			}
		},
		setisManager(state, isManager) {
			state.isManager = isManager
		},
		setAddressList(state, list, page = 0) {
			if (page > 1) {
				state.addressList = [...state.addressList, ...list]
			} else {
				state.addressList = [...list]
			}
		},
		setTotal(state, n) {
			state.total = n
		},
		setUserOrder(state, val) {
			state.userOrder = val
		},
		set_isRole(state, val) {
			state.isRole = val
		},
		set_orderVos(state, val) {
			state.orderVos = val
		}
	},
	actions: {
		// 定义actions，用于修改状态(异步)
		// 刷新token
		getrefreshToken() {
			const refreshToken = uni.getStorageSync('dd-access-network').refreshToken //刷新token
			return apiLogin.refreshToken({
					refreshToken: refreshToken
				})
				.then(res => {
					// const logintime = new Date().getTime()
					// let dataValue = {
					// 	logintime: logintime, //当前登录时间戳
					// 	token: res.data.row.tokenValue, // 设置token值
					// 	refreshToken: res.data.row.refreshToken, //刷新token
					// 	tokenName: res.data.row.tokenName, //token请求头
					// 	timeToken: res.data.row.expireIn //过期时间(秒)
					// }
					// uni.setStorageSync('access-network', dataValue)
				})
		}, //获取用户信息
		async getUpdataUser(context) {
			await apiUser.getUerDetail().then(res => {
				let info = res.data.row.row
				if (res.code == 20000) {
					context.commit('setisManager', info.roleType == 1 || info.roleType == 2)
					context.commit('setStateAttr', info)
				}
			})
		},
		// 获取编码详情
		async getDetail(context, code) {
			uni.showLoading({
				mask: true,
				title: '加载中...'
			})
			await apiCode.getUserOrderCodeInfo({
				code: code
			}).then(res => {
				context.commit('setStatedetailData', res)
			})
			uni.hideLoading()
		},
		// 获取地址列表
		async seleAddress(context, page) {
			let data = {
				"limit": 0,
				"page": page
			}
			uni.showLoading({
				mask: true,
				title: '加载中...'
			})
			await apiAddress.getUserRegisterAddressList(data).then(res => {
				if (res.code == 20000) {
					context.commit('setAddressList', res.data.rows, page)
					context.commit('setTotal', res.data.total)
				}
			})
			uni.hideLoading()
		}
	},
	getters: {
		// getter（获取数据)
		// 处理隐藏手机号
		setProcessingTel(state) {
			let str = state.userInfo.phone
			let tem = str.slice(0, 3) + '****' + str.slice(str.length - 4)
			return tem
		},
	},
	modules: {

	}
});
export default store